Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw on undefined unless defaultValue is defined #46

Merged

Conversation

dferber90
Copy link
Collaborator

@dferber90 dferber90 commented Jan 23, 2025

Flags may not return an undefined value, as undefined can not be serialized.

Specifically, the decide function may not return undefined unless a defaultValue is provided.

When a decide function returns undefined and no defaultValue was provided the flag will throw:

const syncFlag = flag({
  key: '....',
  decide: () => undefined,
});
@vercel/flags: Flag "xxx" must have a defaultValue or a decide function that returns a value

When a decide function returns undefined and a defaultValue was provided the defaultValue will be used:

const syncFlag = flag({
  key: '....',
  decide: () => undefined,
  defaultValue: false
});

By default the decide function's types will not warn when you return undefined as you might have a defaultValue in place. Specify the flag type using flag<boolean> etc to ensure your decide function may not return undefined on a typescript level.

const syncFlag = flag<boolean>({
  key: '....',
  decide: () => undefined, // a typescript warning will now be shown, as we specified the boolean flag type
});

@dferber90 dferber90 requested a review from a team January 23, 2025 13:26
Copy link

vercel bot commented Jan 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
flags-sdk-snippets ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 24, 2025 0:20am
summer-sale ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 24, 2025 0:20am

@dferber90 dferber90 merged commit 2713ea7 into main Jan 24, 2025
12 checks passed
@dferber90 dferber90 deleted the dferber/fla-1641-handle-flags-and-adapters-returning-undefined branch January 24, 2025 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants